<?php
/**
 * @deprecated (暂时不用了，直接使用 ProductCategoryController)
 * 商品模型的管理
 * Author: lyun
 * Date: 2018/3/28
 * Created by Panxsoft.
 */


namespace App\Http\Controllers\Admin\Panx\Product;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Panx\Product\ProductModelStoreRequest;
use App\Http\Requests\Admin\Panx\Product\ProductModelUpdateRequest;
use App\Models\Panx\Category;
use App\Models\Panx\ProductMod;
use Illuminate\Http\Request;

class ProductModelController extends ApiController
{

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $product_models = ProductMod::get();

        return $this->response(compact('product_models'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        // 返回所有的三级商品种类数据
        $categories = Category::where('level', Category::LEVEL_FIRST)->get();

        return $this->response(compact('categories'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  ProductModelStoreRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProductModelStoreRequest $request)
    {
//        $product_model = $this->assembleProductModel($request);
//
//        $stored = ProductMod::create($product_model);
//
//        return $this->response(compact('stored'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

    }

    /**
     * Update the specified resource in storage.
     *
     * @param  ProductModelUpdateRequest  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ProductModelUpdateRequest $request, $id)
    {
//        /** @var ProductMod $product_model */
//        $product_model = ProductMod::find($id);
//            $this->checkItemExist($product_model, '商品模型');
//
//        $product_model_data = $this->assembleProductModel($request);
//
//        $updated = $product_model->update($product_model_data);
//
//        return $this->response(compact('updated'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     * @throws \Exception
     */
    public function destroy($id)
    {
//        /** @var ProductMod $product_model */
//        $product_model = ProductMod::find($id);
//            $this->checkItemExist($product_model, '商品模型');
//            $this->checkModelHasProducts($product_model);
//
//        $deleted = $product_model->delete();
//
//        return $this->response(compact('deleted'));
    }

    /**
     * @param ProductModelStoreRequest|ProductModelUpdateRequest|Request $request
     * @return array
     */
    private function assembleProductModel(Request $request)
    {
        return [
            'customer_id' => ($request->customer_id)?:0, // 从中间件获取
            'category_id' => $request->category_id,
            'name'        => $request->name,
        ];
    }

    /**
     * @param ProductMod $product_model
     *
     * @throws \Exception
     */
    private function checkModelHasProducts(ProductMod $product_model)
    {
        $relate_prod_num = $product_model->products()->count();
        $relate_spec_num = $product_model->specs()->count();
        $relate_attr_num = $product_model->attributes()->count();
        if ($relate_prod_num) {
            $this->requestForceOperate(
                "商品模型已关联{$relate_prod_num}个商品" .
                "，{$relate_spec_num}个规格" .
                "，{$relate_attr_num}个属性" .
                "，请确认强制清除关联并删除商品模型"
            );
        }
    }
}
